﻿using System.Windows;
using System.Windows.Interop;
using WpfPractice.ViewModel;

namespace WpfPractice.View
{
    public partial class CircularMenu : Window
    {
        public CircularMenu()
        {
            InitializeComponent();

            CircularMenuViewModel vm = new CircularMenuViewModel();
            this.DataContext = vm;
            //其实这里更适合使用委托，事件不太好
            vm.CloseRequested += OnCloseRequested;
            //接收消息关闭窗口
            this.Loaded += CircularMenu_Loaded;
        }

        private void CircularMenu_Loaded(object sender, RoutedEventArgs e)
        {
            Point mousePosition = GetMousePosition();
            // 使用转换后的坐标来定位窗口
            this.Left = mousePosition.X - (this.ActualWidth / 2);
            this.Top = mousePosition.Y - (this.ActualHeight / 2);
        }

        private void OnCloseRequested()
        {
            //this.Close();
        }
        private Point GetMousePosition()
        {
            // 获取当前屏幕鼠标位置
            System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
            Point wpfPoint = new Point(point.X, point.Y);

            // 转换屏幕坐标到WPF设备独立坐标
            HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
            System.Windows.Media.Matrix matrix = hwndSource.CompositionTarget.TransformFromDevice;
            return matrix.Transform(wpfPoint);
        }
    }
}
